package com.un.ebs.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.un.ebs.core.config.OSSConfig;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.OSSUtil;
import com.un.ebs.product.domain.ProductImage;
import com.un.ebs.product.enums.ProductImageTypeEnum;
import com.un.ebs.product.mapper.ProductImageMapper;
import com.un.ebs.product.service.ProductImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.un.ebs.product.dto.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LIUBO
 * @since 2019-11-03
 */
@Service
public class ProductImageServiceImpl extends ServiceImpl<ProductImageMapper, ProductImage> implements ProductImageService {

    @Autowired
    private OSSConfig ossConfig;

    @Override
    public String uploadOss(MultipartFile file, String productId, ProductImageTypeEnum imgType) {
        if(com.un.ebs.core.util.StringUtils.isNullOrEmpty(productId)){
            throw new BusinessException("productId 不能为空。");
        }

        String url = OSSUtil.upload(file,String.format("product/%s",productId));
        if(com.un.ebs.core.util.StringUtils.isNullOrEmpty(url)){
            return null;
        }

        try{
            ProductImage img = new ProductImage();
            img.setImgUrl(url);
            img.setProductId(productId);
            img.setImgType(imgType);
            img.setSort(baseMapper.maxSort(productId));
            img.insert();
        }catch (Exception e){
            OSSUtil.delete(url);
            throw new BusinessException("上传失败：" + e.getMessage());
        }
        return url;
    }

    @Override
    public void delete(String id) {
        ProductImage image = baseMapper.selectById(id);
        if(image==null){
            throw new BusinessEntityNotFoundException(id,"product_image");
        }

        OSSUtil.delete(image.getImgUrl());
        //OSSUtil.delete(ossConfig,image.getImgUrl());
        image.deleteById();
    }

    @Transactional
    @Override
    public void deleteByProductId(String productId) {
        LambdaQueryWrapper<ProductImage> lq = Wrappers.lambdaQuery();
        lq.eq(ProductImage::getProductId,productId);
        List<ProductImage> images = baseMapper.selectList(lq);
        for (ProductImage image:images) {
            //OSSUtil.delete(ossConfig,image.getImgUrl());
            OSSUtil.delete(image.getImgUrl());
            image.deleteById();
        }
    }

    @Override
    public void changeSort(String id, int index) {
        ProductImage obj = getById(id);
        obj.setSort(index);
        getBaseMapper().updateById(obj);
    }

    @Override
    public List<ProductImage> queryBySpuId(String id) {
        List<ProductImage> imageList = baseMapper.selectByProductId(id);
        for (ProductImage image : imageList) {
            image.setImgUrl(OSSUtil.getObjectUrl(image.getImgUrl()).toString());
        }
        return imageList;
    }
}